package com.atguigu.tingshu.order.template;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.client.VipServiceConfigFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * title:
 * author: zgl
 * date: 2024/3/16
 * description：
 */
@OrderKinds(OrderType.VIP_ORDER_TYPE)
public class VipConfirm extends OrderConfirm {
    @Autowired
    public VipConfirm(RedisTemplate redisTemplate){
        super(redisTemplate);
    }
    @Resource
    private VipServiceConfigFeignClient vipServiceConfigFeignClient;



    @Override
    public void confirm(OrderInfoVo orderInfoVo,TradeVo tradeVo) {
        if (SystemConstant.ORDER_ITEM_TYPE_VIP.equals(tradeVo.getItemType())) {
            //根据id查询vip套餐信息
            Result<VipServiceConfig> vipServiceConfigResult = vipServiceConfigFeignClient.getVipConfig(tradeVo.getItemId());
            Assert.notNull(vipServiceConfigResult, "订单确认页：获取vip信息失败！");
            VipServiceConfig vipConfig = vipServiceConfigResult.getData();
            Assert.notNull(vipConfig, "订单确认页：获取vip信息失败！");
            //设置订单原始金额
            BigDecimal originalAmount = vipConfig.getPrice();
            orderInfoVo.setOriginalAmount(originalAmount);
            //设置订单总金额
            BigDecimal orderAmount = vipConfig.getDiscountPrice();
            BigDecimal derateAmount = originalAmount.subtract(orderAmount);
            orderInfoVo.setOrderAmount(orderAmount);
            //设置订单减免总金额
            orderInfoVo.setDerateAmount(derateAmount);
            //设置订单明细列表
            List<OrderDerateVo> orderDerateVoList = new ArrayList<>();
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_VIP_SERVICE_DISCOUNT);
            orderDerateVo.setDerateAmount(derateAmount);
            orderDerateVo.setRemarks("vip要迅速到账哦");
            orderDerateVoList.add(orderDerateVo);
            orderInfoVo.setOrderDerateVoList(orderDerateVoList);

            //设置订单减免明细列表
            List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
            OrderDetailVo orderDetailVo = new OrderDetailVo();
            orderDetailVo.setItemId(tradeVo.getItemId());
            orderDetailVo.setItemName("购买vip" + vipConfig.getName());
            orderDetailVo.setItemPrice(orderAmount);
            orderDetailVo.setItemUrl(vipConfig.getImageUrl());
            orderDetailVoList.add(orderDetailVo);
            orderInfoVo.setOrderDetailVoList(orderDetailVoList);
        }
    }
}
